<?php

namespace App\Http\Controllers\Xcx;

use App\Craftsman;
use App\Exceptions\ParamsException;
use App\Exceptions\UpdateException;
use App\Logic\WxLogin;
use App\Member;
use App\Xm_register;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Ionepub\Idcard;
use SmsManager;
use App\Area;
use App\Needwork;
use Illuminate\Support\Facades\Validator;

class UserController extends Controller
{
    public function info()
    {
        $user = auth('xcx')->user();
        $date = $user->toArray();
        $date['work'] = 0;
        $date['reg'] = false;
        if ($user->type == 1) {
            if ($user->members_id) {
                $member = Member::find($user->members_id);
                if ($member) {
                    $craftsman = Craftsman::findOrFail($member->craftmans);
                    $date['work'] = !!$craftsman->work?1:0;
                }
                $date['reg'] = true;
            }
        }else{
            $member = Xm_register::where('uid',$user->id)->first();
            if ($member) {
                $date['reg'] = true;
            }
           
        }
        
       
        return $this->success($date);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function store(Request $request)
    {
        if((new WxLogin())->synchronous(auth('xcx')->user(),$request->post('encryptedData'),$request->post('iv')))
            return $this->success(auth('xcx')->user());
        throw new UpdateException();
    }

    public function auth(Request $request)
    {
//        SmsManager::forgetState();
        $user = auth('xcx')->user();
        $data = $request->only(['mobile', 'address', 'back', 'birthday', 'card', 'face', 'gender', 'nation', 'real_name']);

        $user->fill($data);
        $user->auth = 1;
        $user->save();
        $member = Member::findOrFail($user->members_id);
        if (!empty($data['card'])) {
            $idCard = Idcard::getInstance($data['card']);
            $member->sex = $idCard->getGender() == '男' ? 1 : 2;
            $member->age = $idCard->getAge();
            $member->origin = $idCard->getRegion();

        }
        $member->name = $data['real_name'];
        $member->street = $data['address'];
        $member->save();
        $craftsman = Craftsman::findOrFail($member->craftmans);
        $craftsman->auth = 1;
        $craftsman->level = $craftsman->level+1;
        $craftsman->save();
        return $this->success();
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function type(Request $request)
    {
        $user = auth('xcx')->user();
        $user->type = $request->post('type');
        $user->save();
        return $this->success();
    }

    public function state(){
        $user = auth('xcx')->user();
        $user->state = 1;
        $user->save();
        return $this->success();
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws ParamsException
     */
    public function stated(Request $request){
        $user = auth('xcx')->user();
        if ($user->members_id){
            $member = Member::findOrFail($user->members_id);
            $craftsman = Craftsman::findOrFail($member->craftmans);
            $craftsman->work = !!$request->post('state',0)?1:0;
            $craftsman->save();
            return $this->success();
        }
        throw new ParamsException('请先注册');

    }
    public function create(Request $request)
    {
        $data = $request -> all();
        $preg = '/^1[3-9]{1}[0-9]{9}$/';
        if(preg_match($preg,$data['phone']) != 1){
            return $this->failed(208,'请输入正确的手机号');
        }
        $s = new Xm_register;
        $s -> g_name = $data['gname']; //公司名称
        $s -> name = $data['name']; //项目名称
        $s -> x_province = $data['area'][0]['code']; //项目地址 省
        $s -> x_city = $data['area'][1]['code']; //项目地址 市
        $s -> x_addr = $data['area'][2]['code']; //项目地址 区
        $s -> x_desc = $data['x_desc']; //项目概括
        $s -> x_name = $data['x_name']; //项目总负责人姓名
        $s -> phone = $data['phone']; //项目总负责人电话
        $dta = \DB::table('xm_registers')->where('uid',auth('xcx')->user()->id)->get();
        if(count($dta) != 0) {
            return $this->failed(207,'请勿重复注册');
        }
        $s -> uid = auth('xcx')->user()->id;
        $s -> shenhe = '4';//当前审核为1代表正在审核还尚未通过
        if(auth('xcx')->user()->type == '2'){ //只有项目才能注册项目
            $s -> save();
            return $this->success();
        }else{
            return $this->failed(201,'注册失败');
        }
        
    }
    // public function xmgz()
    // {
    //     $id = auth('xcx')->user()->id;
    //     $s = Xm_register::where('uid',$id)->get();//把当前项目负责人和电话以及项目名称传过去
    //     $arr = [];
    //     foreach ($s as $k=>$v) {
    //         $n = Area::where('id',$v->x_province)->first();
    //         $s = Area::where('id',$v->x_city)->first();
    //         $arr[$k]['x_name'] = $n->name.str_replace('市辖区','',$s->name); 
    //         $arr[$k]['phone'] = $v->phone; // 项目负责人电话
    //         $arr[$k]['name'] = $v->name; // 项目名称
    //     }
    //     return $this->success($arr);
    // }
}
